package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.Prod;
import com.powernode.domain.UserCollection;
import com.powernode.feign.CollectionToProdFeign;
import com.powernode.service.UserCollectionService;
import com.powernode.mapper.UserCollectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection>
        implements UserCollectionService {
    @Autowired
    private UserCollectionMapper userCollectionMapper;
    @Autowired
    private CollectionToProdFeign collectionToProdFeign;


    @Override
    public Page<Prod> getProdList(Page<Prod> page, String wxUserId) {
        Page<UserCollection> userCollectionPage = new Page<>(page.getCurrent(), page.getSize());
        //查询出所以的prodId
        userCollectionPage = userCollectionMapper.selectPage(userCollectionPage, new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, wxUserId)
                .select(UserCollection::getProdId));
        List<UserCollection> records = userCollectionPage.getRecords();
        //如果prodId集合不为空
        if (CollectionUtil.isNotEmpty(records)) {
            //获取所有的prodid
            List<Long> prodIds = records.stream().map(UserCollection::getProdId).collect(Collectors.toList());
            //根据prodId 使用 feign到prod进行查询
            List<Prod> prodList = collectionToProdFeign.loadProdById(prodIds);
            //为该用户传入prodList
            page.setRecords(prodList);
            page.setTotal(userCollectionPage.getTotal());
        }
        return page;
    }

    /**
     * 点击商品收藏
     * 当时收藏状态的时候
     * 点击删除数据库数据
     * 当不是的时候，添加该商品到收集数据库中
     *
     * @param wxUserId
     * @param prodId
     */
    @Override
    public void prodAddOrCancel(String wxUserId, Long prodId) {
        //获得该商品
        Integer count = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, wxUserId)
                .eq(UserCollection::getProdId, prodId));

        if (count > 0) { //此时代表已经收藏 删除该数据
            userCollectionMapper.delete(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getProdId, prodId)
                    .eq(UserCollection::getUserId, wxUserId));
        } else {
            //此时代表为收藏 添加数据
            UserCollection userCollection = new UserCollection();
            userCollection.setCreateTime(new Date());
            userCollection.setProdId(prodId);
            userCollection.setUserId(wxUserId);
            userCollectionMapper.insert(userCollection);
        }
    }
}




